//
//  RemindViewController.m
//  110
//
//  Created by 段报雪 on 16/4/11.
//  Copyright © 2016年 乔砥. All rights reserved.
//

#import "RemindViewController.h"
#import "RemindDetailedViewController.h"
#import "RemindTableViewCell.h"
#import "RemindModel.h"
#import "FMDB.h"
@interface RemindViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *myTableView;

@property (nonatomic, strong)NSMutableArray *dateArray;

@property (nonatomic, strong)FMDatabase *db;

@property (nonatomic, strong)UIImageView *myImageView;

@end

@implementation RemindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"服药提醒";
       self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarButtonItemAction:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_myTableView];
    
    [_myTableView registerClass:[RemindTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    _myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, KWIDTH / 2, KHEIGHT / 3)];
    _myImageView.image = [UIImage imageNamed:@"AlarmBackGround"];
    _myImageView.center = self.view.center;
    [self.view addSubview:_myImageView];
    
}


#pragma mark ---- 返回每个分区多少个Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dateArray.count;
}

#pragma mark ---- 返回具体的Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RemindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    RemindModel *model = _dateArray[indexPath.row];
    //  取消渲染
    cell.selectionStyle = UITableViewCellAccessoryNone;
    cell.nameLabel.text = model.date;
    cell.dateLabel.text = model.name;
    return cell;
}

#pragma mark ---- Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
    RemindDetailedViewController *remindDetailedVC = [[RemindDetailedViewController alloc] init];
    [self.navigationController pushViewController:remindDetailedVC animated:YES];

}


- (void)viewWillAppear:(BOOL)animated
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"date.db"];
    _db = [FMDatabase databaseWithPath:path];
    if (![_db open])
    {
        NSLog(@"无法打开闹钟数据库");
    }

    _dateArray = [NSMutableArray array];
    
    FMResultSet *result = [_db executeQuery:@"SELECT *FROM date"];
    while ([result next])
    {
      RemindModel *model = [[RemindModel alloc]init];
       model.date = [result stringForColumn:@"dateTime"];
       model.name = [result stringForColumn:@"name"];
        [_dateArray addObject:model];
    }
    [result close];

    [self.myTableView reloadData];
    
    
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    RemindModel * model = _dateArray[indexPath.row];
    NSString *deleteSql = [NSString stringWithFormat:
                          @"delete from date where name ='%@'",
                          model.name];
    BOOL res = [_db executeUpdate:deleteSql];
    if (res)
    {
       NSArray * array = [[UIApplication sharedApplication]scheduledLocalNotifications];
        for (UILocalNotification * location in array)
        {
            NSDictionary * dic = location.userInfo;
            if (dic[model.date] != nil)
            {
            [[UIApplication sharedApplication]cancelLocalNotification:location];
            }
            
        }
        [self.dateArray removeObjectAtIndex:indexPath.row];
   
         [tableView reloadData];
        
        
    }
    
    
    
}
- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
